﻿using UnityEngine;
using System.Collections;
using Elixis;
using Elixis.EncryptionOptions;
public class LoadBytes : MonoBehaviour {
    private static AESEncryptor fAesEncrypt = new AESEncryptor( "bytes", AESBits.BITS256 );
    public UITexture res;
    // Use this for initialization
    void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    void OnGUI()
    {
        if( GUI.Button( new Rect( 0, 0, 100, 100 ), "LoadBytes" ) )
        {
            res.mainTexture = TextAssetToTexture( "res/" + "10002" );
        }
    }
    public static Texture TextAssetToTexture( string path )
    {
        TextAsset binAsset = Resources.Load( path, typeof( TextAsset ) ) as TextAsset;
        if( binAsset != null )
        {
            return byte2texture( binAsset.bytes );
        }
        return null;            
    }

    public static Texture2D byte2texture( byte[] bytes )
    {
        if( bytes == null )
        {
            return null;
        }
        byte[] bytesdata = fAesEncrypt.Decrypt( bytes );
        Texture2D t2d;
        t2d = new Texture2D( 0, 0, TextureFormat.RGB24, false );
        t2d.LoadImage( bytesdata );
        t2d.wrapMode = TextureWrapMode.Clamp;
        return t2d;
    }
}
